<?php

namespace Common\Bundle\LibraryBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;

class ThemeType extends AbstractType
{
    private $idTheme;
    private $em;

    public function __construct($idTheme, $em)
    {
        $this->idTheme = $idTheme;
        $this->em = $em;
    }
     /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $em = $this->em->getManager();
        $idTheme = $this->idTheme;
        if($idTheme != 0)
        {
            $builder
                ->add('nom')
                ->add('theme', 'entity',
                    array(
                        'class' => 'Common\Bundle\LibraryBundle\Entity\Theme',
                        'required' => true,
                        'data' => $em->getReference("CommonLibraryBundle:Theme",$idTheme)
                    ))
            ;
        }
        else
        {
            $builder
                ->add('nom')
                ->add('theme')
            ;
        }
    }
    
    /**
     * @param OptionsResolverInterface $resolver
     */
    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Common\Bundle\LibraryBundle\Entity\Theme'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'common_bundle_librarybundle_theme';
    }
}
